import { api } from './api'
import type { ApiResponse } from '../types'

interface UploadedFile {
  id: number
  filename: string
  originalName: string
  mimeType: string
  size: number
  url: string
  thumbnailUrl?: string
  metadata?: Record<string, any>
  createdAt: string
}

interface UploadConfig {
  maxSize: number
  allowedTypes: string[]
  uploadPath: string
}

export const uploadApi = api.injectEndpoints({
  endpoints: (builder) => ({
    uploadFile: builder.mutation<ApiResponse<UploadedFile>, FormData>({
      query: (data) => ({
        url: '/upload',
        method: 'POST',
        body: data,
      }),
    }),

    uploadImage: builder.mutation<ApiResponse<UploadedFile>, FormData>({
      query: (data) => ({
        url: '/upload/image',
        method: 'POST',
        body: data,
      }),
    }),

    uploadAvatar: builder.mutation<ApiResponse<UploadedFile>, FormData>({
      query: (data) => ({
        url: '/upload/avatar',
        method: 'POST',
        body: data,
      }),
      invalidatesTags: ['User'],
    }),

    uploadArticleCover: builder.mutation<ApiResponse<UploadedFile>, FormData>({
      query: (data) => ({
        url: '/upload/article-cover',
        method: 'POST',
        body: data,
      }),
    }),

    deleteFile: builder.mutation<ApiResponse<void>, string>({
      query: (fileId) => ({
        url: `/upload/${fileId}`,
        method: 'DELETE',
      }),
    }),

    getUploadConfig: builder.query<ApiResponse<UploadConfig>, void>({
      query: () => '/upload/config',
    }),

    getUploadToken: builder.query<ApiResponse<{ token: string; expires: number }>, void>({
      query: () => '/upload/token',
    }),
  }),
})

export const {
  useUploadFileMutation,
  useUploadImageMutation,
  useUploadAvatarMutation,
  useUploadArticleCoverMutation,
  useDeleteFileMutation,
  useGetUploadConfigQuery,
  useGetUploadTokenQuery,
} = uploadApi 